package com.ld.collegetask.login;

import android.content.Intent;
import android.databinding.DataBindingUtil;
import android.graphics.Color;
import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.util.Log;
import android.view.View;

import com.google.gson.Gson;
import com.ld.collegetask.BaseActivity;
import com.ld.collegetask.MainActivity;
import com.ld.collegetask.MemberForgetPasswordFindUserActivity;
import com.ld.collegetask.MemberUsernameUniqueActivity;
import com.ld.collegetask.R;
import com.ld.collegetask.beans.BaseJsonBean;
import com.ld.collegetask.beans.MemberBean;
import com.ld.collegetask.databinding.ActivityLoginBinding;
import com.ld.collegetask.serverInterface.RequestSerives;
import com.ld.collegetask.utils.AppApplication;
import com.ld.collegetask.utils.SharePreference;
import com.ld.collegetask.utils.ToastUtil;

import cn.jpush.im.android.api.JMessageClient;
import cn.jpush.im.api.BasicCallback;
import me.leefeng.promptlibrary.PromptDialog;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.scalars.ScalarsConverterFactory;

public class LoginActivity extends BaseActivity {
    private PromptDialog promptDialog;
    private ActivityLoginBinding binding;
    private String username;
    private String password;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = DataBindingUtil.setContentView(this,R.layout.activity_login);
        promptDialog = new PromptDialog(this);
        setTitleView(this,binding.ilTitle,false,true,false,false);
        setTitle("登录");
        findView();
    }

    @Override
    public void loading() {

    }

    private void findView() {
        binding.loginSubmitButton.setOnClickListener(this);
        binding.registerButton.setOnClickListener(this);
        binding.forgetPassword.setOnClickListener(this);
        if (binding.loginUsername.getText().toString().trim().length() > 0) {
            binding.loginSubmitButton.setBackgroundColor(Color.parseColor("#40D15C"));
            binding.loginSubmitButton.setEnabled(true);
        } else {
            binding.loginSubmitButton.setBackgroundColor(Color.parseColor("#CCCCCC"));
            binding.loginSubmitButton.setEnabled(false);
        }
        binding.loginUsername.addTextChangedListener(new TextWatcher() {

            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                // 输入的内容变化的监听
                if (s.toString().trim().length() > 0) {
                    binding.loginSubmitButton.setBackgroundColor(Color.parseColor("#40D15C"));
                    binding.loginSubmitButton.setEnabled(true);
                } else {
                    binding.loginSubmitButton.setBackgroundColor(Color.parseColor("#CCCCCC"));
                    binding.loginSubmitButton.setEnabled(false);
                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
    }

        private void member_login() {
        Retrofit retrofit = new Retrofit.Builder().baseUrl(AppApplication.server).addConverterFactory(ScalarsConverterFactory.create()).build();
        RequestSerives requestSerives = retrofit.create(RequestSerives.class);
        Call<String> call = requestSerives.member_login(username, password);
        call.enqueue(new Callback<String>() {
            @Override
            public void onResponse(Call<String> call, Response<String> response) {
                Gson gson = new Gson();
                BaseJsonBean base = gson.fromJson(response.body().toString(), BaseJsonBean.class);
                if ("1".equals(base.getCode())) {
                    final MemberBean member = gson.fromJson(base.getJson(), MemberBean.class);
                    JMessageClient.login(username, password, new BasicCallback() {
                        @Override
                        public void gotResult(int i, String s) {
                            Log.i("im",i+"*****"+s);
                            if (i==0){
                                SharePreference sp = new SharePreference(getBaseContext());
                                sp.setUserId(member.getUserId());
                                sp.setImId(username);
                                sp.setImPassword(password);
                                sp.setLoginState(true);
                                Intent intent = new Intent(LoginActivity.this, MainActivity.class);
                                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK |
                                        Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                startActivity(intent);
                                finish();
                                promptDialog.dismiss();
                            }else {
                                promptDialog.showWarn("登陆失败");
                            }
                        }
                    });
                } else {
                    promptDialog.showWarn(base.getMsg());
                }
            }

            @Override
            public void onFailure(Call<String> call, Throwable t) {
                ToastUtil.showLong(mActivity,"网络错误，请检查网络");
                promptDialog.dismiss();
            }
        });
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        switch (v.getId()){
            case R.id.login_submit_button:
                username = binding.loginUsername.getText().toString();
                password = binding.loginPassword.getText().toString();
                promptDialog.showLoading("正在登录");
                member_login();
                break;
            case R.id.register_button:
                startActivity(new Intent(LoginActivity.this, MemberUsernameUniqueActivity.class));
                break;
            case R.id.forget_password:
                startActivity(new Intent(LoginActivity.this, MemberForgetPasswordFindUserActivity.class));
                break;
        }
    }
}
